package dao;

import java.io.FileWriter;
import java.util.*;

import org.dom4j.*;
import org.dom4j.io.*;

import po.*;
import util.PathTool;

public class UserDao {
	
	private static String tableName;
	private static String xmlPathr;
	private static Document document;
	
	
	static {
		tableName = "user";
		xmlPathr = PathTool.getWebPath() + "xmlData/" + tableName + ".xml";
		try {
			SAXReader sr = new SAXReader();
	        document = sr.read(xmlPathr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加一条记录
	 * 
	 * @param user
	 * @return
	 */
	public static boolean addOneRecord(User user) { // 添加一个用户

		
		Element root = document.getRootElement();
		Element newE = root.addElement(tableName);
		newE.addAttribute("id", user.getId());
		Element pw = newE.addElement("pw");
		pw.setText(user.getPw());
		writeToNewXMLDocument();
		return true;
	}

	public static boolean updataOneRecord(User user) { // 更新一条记录
		String id = user.getId();
		String pw = user.getPw();
		Element root = document.getRootElement();
		List<Element> users = root.elements();
		Element tempe;
		for (int i=0; i<users.size(); i++) {
			tempe = users.get(i);
			if (id.equals(tempe.attributeValue("id"))) {
				for (Iterator<Element> it = tempe.elementIterator(); it.hasNext();) {
                    Element node =  it.next();
                    String type = node.getName();
                    if ("pw".equals(type)) {
                        node.setText(pw);
                    }
                }
			}
		}
		writeToNewXMLDocument();
		return true;
	}
	
	public static User[] queryAllRecord() { // 查询所有
		Element root = document.getRootElement();
		List<Element> list = root.elements();
		Element tempe;
		int amount = list.size();
		User[] users = new User[amount];
		for (int i=0; i<list.size(); i++) {
			tempe = list.get(i);
			User tempUser = new User();
			tempUser = new User();
			tempUser.setId(tempe.attributeValue("id"));
			for (Iterator<Element> it = tempe.elementIterator(); it.hasNext();) {
                Element node =  it.next();
                String type = node.getName();
                if ("pw".equals(type)) {
                	tempUser.setPw(node.getStringValue());
                }
            }
			users[i] = tempUser;
		}
		return users;
	}
	
	public static boolean queryOneRecord(User u) { // 根据 主码 查询一条数据
		String id = u.getId();
		System.out.println(xmlPathr);
		Element root = document.getRootElement();
		List<Element> userList =  root.elements(); // 所有记录组成的集合
		// 开始查找对应的 id
		Element temp;
		for (int i=0; i<userList.size(); i++) {
			temp = (Element)userList.get(i); // 一条记录
			// 判断 id 是否为需要的
			if (id.equals(temp.attributeValue("id"))) {
				List<Element> userElementList = temp.elements();
				for (int j=0; j<userElementList.size(); j++) {
					Element te = (Element)userElementList.get(j);
					if ("pw".equals(te.getName())) {
						u.setPw(te.getText());
					}
				}
				return true;
			}
		}
		return false; // 没找到
	}
	
		

    /**
     * 通过document对象将内存中的dom树保存到新的xml文档。
     * 
     * @param document
     * @throws Exception
     */
    private static void writeToNewXMLDocument() {
    	try {
            XMLWriter writer = new XMLWriter(new FileWriter(
            		xmlPathr));
            writer.write(document);
            writer.close();
    	} catch(Exception e) {
    		e.printStackTrace();
    	}
    }


}
